实体类:
using System;using System.Collections.Generic;using System.Linq;using System.Web;////// gouwu 的摘要说明/// public class gouwu{ public gouwu() { // // TODO: 在此处添加构造函数逻辑 // } public int ids { get; set; } public string pic { get; set; } public string name { get; set; } public decimal nowprice { get; set; } public decimal oldprice { get; set; } public string context { get; set; }}
数据访问类:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data.SqlClient;////// gouwudata 的摘要说明/// public class gouwudata{ SqlConnection conn = null; SqlCommand cmd = null; public gouwudata() { conn = new SqlConnection("server=.;database=data0928;user=sa;pwd=123"); cmd = conn.CreateCommand(); } public Listselect() { List glist = new List (); cmd.CommandText = "select*from gouwu"; conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.HasRows) { while (dr.Read()) { gouwu g = new gouwu(); g.ids = Convert.ToInt32(dr[0]); g.pic = dr[1].ToString(); g.name = dr[2].ToString(); g.nowprice = Convert.ToDecimal(dr[3]); g.oldprice = Convert.ToDecimal(dr[4]); g.context = dr[5].ToString(); glist.Add(g); } } conn.Close(); return glist; } public void delete(int ids) { cmd.CommandText = "delete from gouwu where ids='"+ids+"'"; conn.Open(); cmd.ExecuteNonQuery(); conn.Close(); }}
aspx里:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
cs里:
Repeater的Command操作
1、ItemCommand事件 :在Repeater中所有能触发事件的控件,都会来触发这一个事件
后台创建:在Page_Load中 Repeater1.ItemCommand += ,然后双击Tab键创建
2、CommandName : 判断点击的是什么按钮,
后台调用:e.CommandName
3、CommandArgument : 触发事件所传递过来的数据,放在这里面 界面值绑定时要用 单引号 !!!!!! 不同的name可以有不同的commandarguement值,在判断对应name时,可以取到对应的Arguement值 if (e.CommandName == "delete")
后台调用:e.CommandArgument
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Repeater1.DataSource = new gouwudata().select(); Repeater1.DataBind(); } Repeater1.ItemCommand += Repeater1_ItemCommand; } void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "delete")//repeater的command方法 { new gouwudata().delete(Convert.ToInt32(e.CommandArgument));//repeater的command方法 Repeater1.DataSource = new gouwudata().select();//删除后及时刷新数据 Repeater1.DataBind(); } }}
如何不用repeater展示数据:
aspx中:用literal
cs中:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Literal1.Text = DataBind(); } } public string DataBind() { string end = ""; Listglist = new gouwuData().Select(); foreach (gouwu g in glist) { if (g.name == "猕猴桃") { continue; } end += " "; end += " "; end += ""; } return end; }}" + g.name + ""; end += "价格:" + g.nowPrice + " " + g.oldPrice + ""; end += "" + g.context + ""; end += " 删除"; end += "